<?php
class taoguaninventory_products_import {

    function run(&$cursor_id,$params){
        //$pObj      = &app::get($params['app'])->model($params['mdl']);
        //$branchObj = &app::get('ome')->model('branch');
        $productsObj = &app::get('ome')->model('products');
        $branchPosObj = &app::get('ome')->model('branch_pos');
        $branchProductObj = &app::get('ome')->model('branch_product');
        $inventoryItemsObj = &app::get('taoguaninventory')->model('inventory_items');
        $inventoryObj = &app::get('taoguaninventory')->model('inventory');
        $branch_id = $params['sdfdata']['branch_id'];
        $branch    = $params['sdfdata']['branch'];
        $inv_id    = $params['sdfdata']['inv_id'];
        $total = 0;
        foreach ($params['sdfdata']['products'] as $v){
            $inv_item = array();
            $product = $productsObj->dump($v['product_id'],'product_id,store');
            if ($product){
                    $inv_item['inventory_id'] = $inv_id;
                    $inv_item['product_id'] = $v['product_id'];
                    $inv_item['pos_id'] = 0;
                    $inv_item['name'] = $v['name'];
                    $inv_item['bn'] = $v['bn'];
                    $inv_item['spec_info'] = $v['spec_info'];
                    $inv_item['unit'] = $v['unit'];
                    $inv_item['pos_name'] = '';
                    $inv_item['accounts_num'] = $v['store'];
                    $inv_item['actual_num'] = $v['num'];
                    $inv_item['shortage_over'] = $v['num']-$v['store'];
                    $inv_item['price'] = $v['price'];
                    $inv_item['availability'] = 'true';
                    $inv_item['memo'] = $v['condition'];
                    
                    $total += $inv_item['shortage_over']*$v['price'];
                    $inventoryItemsObj->save($inv_item);//记录导入明细
                    
            }
        }
        
   	  //if(kernel::single('taoguaninventory_inventory')->do_iostock($inv_id,$msg)){
   	  		$inv = array();
    	    $inv['inventory_id'] = $inv_id;
	        $inv['difference'] = $total;//$tmp['total'];
	        $inv['import_status'] = '2';
	        $inv['update_status'] = '2';
	        
	        
	        $inventoryObj->save($inv);
    	//}
      
        return false;
    }
}
